﻿package mortal.game.view.copy.escort
{
    import Message.Public.*;
    import com.mui.utils.*;
    import flash.events.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.game.view.common.*;
    import mortal.game.view.copy.escort.vo.*;
    import mortal.game.view.skill.*;
    import mortal.game.view.skill.panel.render.*;
    import mortal.mvc.core.*;

    public class EscortCopyEscortBar extends EscortCopyShortcutBarBase
    {
        private var _skillItem:SkillItem;
        private var _lifeBar:BaseProgressBar;
        private var _entityInfo:EntityInfo;
        private static var _instance:EscortCopyEscortBar;

        public function EscortCopyEscortBar(param1:String)
        {
            _titleName = param1;
            return;
        }// end function

        private function onUseSkillHandler(event:MouseEvent) : void
        {
            var _loc_2:EscortCopyOperVo = null;
            if (this._skillItem.dragSource)
            {
                _loc_2 = new EscortCopyOperVo(EEscortOper._EEscortOperUseSkill);
                _loc_2.extend = SkillInfo(this._skillItem.dragSource).skillId;
                if (ThingUtil.selectEntity)
                {
                    _loc_2.entityId = ThingUtil.selectEntity.entityInfo.entityInfo.entityId;
                }
                Dispatcher.dispatchEvent(new DataEvent(EventName.EscortCopyOper, _loc_2));
            }
            return;
        }// end function

        private function onSceneAddSelfCar(event:DataEvent) : void
        {
            this._entityInfo = event.data as EntityInfo;
            if (this._entityInfo)
            {
                this._entityInfo.addEventListener(EntityInfoEventName.UpdateLife, this.updateLife);
            }
            return;
        }// end function

        private function updateLife(event:Event = null) : void
        {
            this._lifeBar.setValue(this._entityInfo.entityInfo.life, this._entityInfo.entityInfo.maxLife);
            return;
        }// end function

        override public function update(param1:Object, ... args) : void
        {
            var _loc_4:SEscortSkill = null;
            var _loc_5:SkillInfo = null;
            args = param1 as Array;
            if (args && args.length > 0)
            {
                _loc_4 = args[0] as SEscortSkill;
                _loc_5 = new SkillInfo();
                _loc_5.tSkill = SkillConfig.instance.getInfoById(_loc_4.skillId);
                this._skillItem.setSkillInfo(_loc_5);
            }
            else
            {
                this._skillItem.setSkillInfo(null);
            }
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._skillItem = UIFactory.getUICompoment(SkillItem, 108, 14, this);
            this._skillItem.setSize(38, 38);
            this._skillItem.setBg(ImagesConst.PackItemBg);
            this._skillItem.configEventListener(MouseEvent.CLICK, this.onUseSkillHandler);
            this._lifeBar = UICompomentPool.getUICompoment(BaseProgressBar);
            this._lifeBar.createDisposedChildren();
            this._lifeBar.setBg(ImagesConst.PetLifeBg, true, 100, 12);
            this._lifeBar.setProgress(ImagesConst.PetExp, true, 1, 1, 99, 16);
            this._lifeBar.setLabel(BaseProgressBar.ProgressBarTextPercent, 15, -4, 75, 8);
            this._lifeBar.mouseChildren = false;
            this._lifeBar.mouseEnabled = true;
            this._lifeBar.setValue(100, 100);
            UIFactory.setObjAttri(this._lifeBar, 75, 56, -1, -1, this);
            Dispatcher.addEventListener(EventName.SceneAddSelfCar, this.onSceneAddSelfCar);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            Dispatcher.addEventListener(EventName.SceneAddSelfCar, this.onSceneAddSelfCar);
            if (this._entityInfo)
            {
                this._entityInfo.removeEventListener(EntityInfoEventName.UpdateLife, this.updateLife);
            }
            this._lifeBar.dispose(param1);
            this._skillItem.dispose(param1);
            this._lifeBar = null;
            this._skillItem = null;
            this._entityInfo = null;
            return;
        }// end function

        public static function get instance() : EscortCopyEscortBar
        {
            return _instance;
        }// end function

        public static function get hasInstance() : Boolean
        {
            return _instance != null;
        }// end function

    }
}
